//
//  AllBankCarListVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/7.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class AllBankCarListVC: BaseViewController {
    
    var dataModels : [BankCardInfoModel]?
    
    var block : ((_ bankName : String,_ bankCode : String) -> ())?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "选择银行卡类型"
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "确定", style: .plain, target: self, action: #selector(dissMissYou))
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: BankImgNameCell.self), bundle: nil), forCellReuseIdentifier: BankImgNameCell.identifer())
        
        plainTableView.tableFooterView = UIView()
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()


        //
    }
    
    
    func dissMissYou() {
        
        for item in dataModels! {
            
            if item.isSelect == true {
                
                if block != nil {
                    
                    block!(item.bank_name,item.bank_code)
                    
                }
                
                
            }
        }
        
        
        
        navigationController?.popViewController(animated: true)
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        SVProgressHUD.dismiss()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}


extension AllBankCarListVC{
    
    func loadData() {
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=index&a=get_bank_list", parameters: nil) { [weak self] (responseObject, error)  in
            
            
            if error == nil {
                
                self?.dataModels = [BankCardInfoModel]()
                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(BankCardInfoModel(dic: dic))
                    
                }
                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
    }
    
    
    
    
    
}




// MARK:- tableview代理方法
extension AllBankCarListVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return dataModels?.count ?? 0
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : BankImgNameCell =  tableView.dequeueReusableCell(withIdentifier: BankImgNameCell.identifer()) as! BankImgNameCell
        
        tableCell.model = dataModels?[indexPath.row]
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 60
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let model = dataModels![indexPath.row]
        
        if  model.isSelect  == true {
            return
        }
        
        
        for item in dataModels! {
            item.isSelect = false
        }
        
        model.isSelect = true
        
        
        plainTableView.reloadData()
        
        
        
    }
    
    
    
}


